//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IO_CSTRINGSTREAM_H_
#define _BUOLA_IO_CSTRINGSTREAM_H_

#include <buola/io/cbufferstream.h>

namespace buola { namespace io {

class CStringStream : public CBufferStream
{
public:
    CStringStream()
    {
        mWPtr=mRPtr=mBegin=mREnd=mStaticBuffer;
        mWEnd=mBegin+sizeof(mStaticBuffer);
        mLength=0;
    }

public:
    virtual ~CStringStream()
    {
        if(mBegin!=mStaticBuffer)
            free(mBegin);
    }
    
private:
    ssize_t DoRead(uint8_t *pData,size_t pLen) final
    {
        //update length
        mLength=std::max(mLength,mWPtr-mBegin);
        mREnd=mBegin+mLength;
        ssize_t lAvailable=mREnd-mRPtr;
        if(lAvailable>=pLen)
        {
            memcpy(pData,mRPtr,pLen);
            mRPtr+=pLen;
            return pLen;
        }
        else
        {
            memcpy(pData,mRPtr,lAvailable);
            mRPtr=mREnd;
            return lAvailable;
        }
    }
    
    ssize_t DoWrite(const uint8_t *pData,size_t pLen) final
    {
        ssize_t lWOffset=mWPtr-mBegin;
        ssize_t lROffset=mRPtr-mBegin;
        //update length
        mLength=std::max(mLength,lWOffset);
        //create new buffer and copy old and new data
        ssize_t lNewSize=(mWEnd-mBegin+pLen)*2;
        uint8_t *lNewBuffer=alloc_array<uint8_t>(lNewSize);
        memcpy(lNewBuffer,mBegin,mLength);
        memcpy(lNewBuffer+lWOffset,pData,pLen);
        //free old buffer if it was dynamic
        if(mBegin!=mStaticBuffer) free(mBegin);
        //update all pointers
        mBegin=lNewBuffer;
        mWPtr=mBegin+lWOffset+pLen;
        mWEnd=mBegin+lNewSize;
        mLength=std::max(mLength,lWOffset+pLen);
        mRPtr=mBegin+lROffset;
        mREnd=mBegin+mLength;
        
        return pLen;
    }
    
protected:
    uint8_t mStaticBuffer[32];
    uint8_t *mBegin;
    size_t mLength; //length of valid data
};

/*namespace io*/ } /*namespace buola*/ }

#endif
